/**
 * 
 */
package com.geezdata.cps.ttk.core.command;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.geezdata.cps.base.response.BaseResponse;
import com.geezdata.cps.cqrs.command.Command;
import com.geezdata.cps.sdk3rd.wechat.response.WechatLoginResult;
import com.geezdata.cps.ttk.common.enums.TtkErrorCodeEnum;
import com.geezdata.cps.ttk.common.request.CreateOrUpdateOpenUserRequest;
import com.geezdata.cps.ttk.common.response.CreateOrUpdateOpenUserResult;
import com.geezdata.cps.ttk.domain.domain.OpenUser;
import com.geezdata.cps.ttk.domain.factory.OpenUserFactory;
import com.geezdata.cps.ttk.integration.wechat.WechatloginService;
import com.geezdata.cps.ttk.mapper.mapper.OpenUserMapper;


@Service
public class CreateOrUpdateOpenUserCommand implements Command<CreateOrUpdateOpenUserRequest, CreateOrUpdateOpenUserResult>{
    private static final Logger logger = LoggerFactory.getLogger(CreateOrUpdateOpenUserCommand.class);
    @Autowired
    private OpenUserMapper openUserMapper;

    @Autowired
    private WechatloginService wechatloginService;

    @Transactional
	@Override
	public BaseResponse<CreateOrUpdateOpenUserResult> execute(CreateOrUpdateOpenUserRequest request) {
    	CreateOrUpdateOpenUserResult result =new CreateOrUpdateOpenUserResult();
        OpenUser openUse = new OpenUser();
        WechatLoginResult res = wechatloginService.getWechatLogin(request.getCode());
        openUse.setSessionKey(res.getSessionKey());
        openUse.setOpenId(res.getOpenId());
        result.setOpenId(res.getOpenId());
        if(res.getOpenId()==null) {
            logger.error("获取unionId出错，检查appId等配置");
            return BaseResponse.error(TtkErrorCodeEnum.CODE_EXPIRED.getCode(), TtkErrorCodeEnum.CODE_EXPIRED.getDesc());
        }
        result.setSessionKey(res.getSessionKey());
        OpenUser existOpenUser= testIfExistOpenUser(res);
        OpenUser openUser = OpenUserFactory.newInstance(openUse,existOpenUser);
        createOrUpdateOpenUser(existOpenUser,openUser);
        result.setOpenUserId(openUser.getOpenUserId());
		return BaseResponse.success(result);
	}

    private OpenUser testIfExistOpenUser(WechatLoginResult result) {
        OpenUser exsitSharer = null;
        OpenUser condition =new OpenUser();
        if(result.getOpenId()!=null&&result.getSessionKey()!=null) {
            condition.setOpenId(result.getOpenId());
            condition.setSessionKey(result.getSessionKey());
            exsitSharer = openUserMapper.selectOne(condition);
        }
        return exsitSharer;
    }
    private void createOrUpdateOpenUser(OpenUser existOpenUser, OpenUser openUser) {
        if (existOpenUser == null) {
            openUserMapper.insert(openUser);
        } else {
            openUserMapper.updateByPrimaryKey(openUser);
        }
    }
}
